//
//  TitleHeadView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/10/30.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

@objc protocol TitleHeadViewDelegate {
    @objc optional func selectItem(index:Int) //选择标签
}

class TitleHeadView: UIView {
    
    weak var delegate : TitleHeadViewDelegate?
    
    fileprivate let cellId = "titleCell"
    
    var dataArray = [String]() {
        didSet{
           collectionView.reloadData()
        }
    }
    
    let moreButton = UIButton()
    
    fileprivate var collectionView : UICollectionView!

    init() {
        super.init(frame: .zero)
        
        moreButton.layer.contents = #imageLiteral(resourceName: "bottom_arrow").cgImage
        self.addSubview(moreButton)
        
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        layout.minimumInteritemSpacing = RATIO(maxNum: 20)
        collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.white
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(TitleHeadCell.self, forCellWithReuseIdentifier: cellId)
        self.addSubview(collectionView)
        
        moreButton.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(RATIO(maxNum: -10))
            make.centerY.equalToSuperview()
            make.width.height.equalTo(RATIO_H(maxNum: 20))
        }
        
        collectionView.snp.makeConstraints { (make) in
            
            make.left.top.bottom.equalToSuperview()
        make.right.equalTo(moreButton.snp.left).offset(RATIO(maxNum: -10))
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension TitleHeadView : UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as? TitleHeadCell
        cell?.titleLabel.text = dataArray[indexPath.row]
        return cell!
        
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        let title = dataArray[indexPath.row]
        let size = title.size(font: RATIO(maxNum: 16), height: RATIO_H(maxNum: 40))
        return size
        
        
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)
        
        self.delegate?.selectItem!(index: indexPath.row)
    }
    
}
